package com.handtransaction.dao;


import com.handtransaction.entity.Goods;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsDao {
    /**
     * 增加商品
     *
     * @param goods
     * @return
     */
    @Insert("INSERT INTO goods(gname,guid,gprice,gnumber,gcid,gdetail,guname,gimg_path) " +
            "VALUES(#{gname},#{guid},#{gprice},#{gnumber},#{gcid},#{gdetail},#{guname},#{gimg_path})")
    public int addGoods(Goods goods);

    @Insert("INSERT INTO goods(gname,guid,gprice,gnumber,gcid,gdetail,guname,gimg_path,gtinyint) " +
            "VALUES(#{gname},#{guid},#{gprice},#{gnumber},#{gcid},#{gdetail},#{guname},#{gimg_path},#{gtinyint})")
    public int addGoods2(Goods goods);

    /**
     * 删除商品
     *
     * @param gid
     * @return
     */
    @Delete("DELETE FROM goods WHERE gid=#{gid}")
    public int deleteGoodsByGid(Integer gid);


    /**
     * 根据gid修改商品
     *
     * @param goods
     * @return
     */
    @Update("UPDATE goods SET gname=#{gname},gprice=#{gprice},gcid=#{gcid},gdetail=#{gdetail},gimg_path=#{gimg_path} WHERE gid=#{gid}")
    public int updateGoods(Goods goods);

    /**
     * 处理买了商品后，修改商品的tinyint字段从而实现不在主页上显示
     */
    @Update("UPDATE goods SET gtinyint = 0 WHERE gid=#{gid}")
    public int updategtinyint(Goods goods);


    //    查询商品

    /**
     * 根据 商品id 查询商品
     *
     * @param gid
     * @return
     */
    @Select("SELECT * FROM goods WHERE gid=#{gid} ")
    public Goods queryGoodBygid(Integer gid);

    @Select("SELECT * FROM goods WHERE gtinyint = 1")
    public List<Goods> queryGoods();


    /**
     * 根据 商品名称关键字 查询商品
     *
     * @param gmessage
     * @return
     */
    //导入的gmessage加工成”%“+gmessage+”%“
    /*
    @Select("SELECT* FROM goods" +
            "WHERE (gname LIKE #{gmessage}) OR gdetail LIKE #{gmessage}")
     */
    @Select("SELECT* FROM goods " + "WHERE gname LIKE #{gmessage} or gdetail like #{gmessage} and gtinyint = 1")
    public List<Goods> queryGoodsBygm(String gmessage);

    @Select("SELECT* FROM goods WHERE MATCH(gname,gdetail) against(#{gmessage} "
                         /*
 下面这个语句是在原来搜索的结果上再拓展搜索结果，比如上面的语句搜索到
 （gname="mysql"，gdetail="哈哈哈哈"）
 加了下面的语句后就多了gname或gdetail 包含mysql或“哈哈哈哈”两个字段以上的记录
 比如多了(gname="123546"，gdetail="my")
  */
//+ "with query expansion"
            +" IN BOOLEAN MODE) and gtinyint = 1"
    )
    public List<Goods> queryGoodsBygm1(String gmesage);

    /**
     * 根据 类别 查找商品
     *
     * @param gcid
     * @return
     */
    @Select("SELECT* FROM goods WHERE gcid=#{gcid} and gtinyint = 1")
    public List<Goods> queryGoodsBygcid(String gcid);

    /**
     * 根据用户id查找商品
     *
     * @param guid
     * @return
     */
    @Select("SELECT * FROM `goods` WHERE guid = #{guid}")
    public List<Goods> queryGoodsByGuid(int guid);


}